<template>
  <el-button :size="props.size" type="primary" @click="openAdd=true">新增</el-button>
  <el-dialog v-model="openAdd" title="新增"
             @close="resetForm"
             width="60%">
    <avue-form
        ref="formRef"
        :option="levelFormOption"
        @submit="submit"
        v-model="form"></avue-form>
  </el-dialog>
</template>

<script setup>
import {levelFormOption} from "@/views/system/level/js/levelOptions.js";
import {defineEmits, ref} from 'vue';
import {levelSave} from "@/views/system/level/js/levelRequest.js";

const emit = defineEmits(['reflashPage'])

const props = defineProps({
  size: {
    type: String,
    default: 'default',
  }
})

const formRef = ref(null)
const form = ref({});
const openAdd = ref(false)

const success = ref(false)

const resetForm = () => {
  formRef.value.resetForm()
}

const submit = (form, done) => {
  levelSave(form).then(res => {
    if (res.success) {
      success.value = true
    }
  }).finally(() => {
    if (success.value) {
      openAdd.value = false
      resetForm()
    }
    emit('reflashPage')
    done()
  })
}
</script>

<style scoped>

</style>